﻿$(function(){//延迟加载
	var parentId='';
	var newnode=null;
	var failuretypeId='';
	$('#dptree').tree({
		url:'getrootdf',
		onClick:function(node){
			failuretypeId=node.id;

			$('#failurelist').datagrid('reload',{
				failuretypeId:failuretypeId
			});
		},
		onContextMenu: function (e, node) {
	        e.preventDefault();
	        parentId=node.id; 
	        failuretypeId=node.id;
	        $("#tabsMenu").menu('show', {
	            left: e.pageX,
	            top: e.pageY
	        });      
	    }
	});
	$('#dptree').tree({
	    onExpand:function(node){
			if (node){
	    		var cnode=$('#dptree').tree('getChildren',node.target);
	    		//alert(cnode);
	    		if(cnode!=''){
//		    		alert(cnode[0].id);
		    		for(var i=0;i<cnode.length;i++){
		    			$('#dptree').tree('remove',cnode[i].target);
		    		}		    			
	    		}
				$.ajax({
					url:'extendfailurenode?nodeId='+node.id,
					success:function(data){
						$('#dptree').tree('append', {
							parent: node.target,
							data:data
						});
					}
				});
			}
		}
	});
	
	$('#loadfailurelist').load('tofailurelist',{
		
	},function(){
		$.getScript('/xasj/js/failure/failurelist.js');
	});
	
	$('#addnewfailuretype').on('click',function(){
		adddg(parentId);
	});
	
	$('#deletefailuretype').on('click',function(){
		alert(failuretypeId);
		if(failuretypeId==''){
			alert('未选择');
			return;
		}
		$.ajax({
			url:'deletefailuretype',
			type:'POST',
			data:{
				failuretypeId:failuretypeId
			},
			success:function(data){
				alert(data.msg);
			}
		});
	});
	
	function adddg(parentId){		
		$('#adddg').dialog({
			title:'添加子故障类型',
			width:'330px',
			height:'150px',
			modal: true
		});
		$('#adddg').load('toaddfailuretype?parentId='+parentId,function(){
			$.getScript('/xasj/js/failure/addfailuretype.js');
		});
	}
	
	function addu(failuretypeId){		
		$('#afailure').dialog({
			title:'添加故障',
			width:'330px',
			height:'199px',
			modal: true,
			collapsible:true
		});
		$('#afailure').load('toaddfailure?failuretypeId='+failuretypeId,function(){
			$.getScript('/xasj/js/failure/addfailure.js');
		});
	}
	
	$('#addnewfailure').on('click',function(){
		addu(failuretypeId);
	});
});